import {
  getAccountTable,
  editAccountRecord,
  editSysAccountRecord
} from '../../api/system'
import {
  ACCOUNT_LIST,
  ACCOUNT_CURRENT_ROW,
  ACCOUNT_DATA_COUNT,
  ACCOUNT_HISTORY_DATA,
  ACCOUNT_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    accountList: [],
    accountDataCount: null,
    accountPageSize: 20,
    accountHistoryData: [],
    row: {
      ctime: '',
      name: '',
      phone: '',
      upwd: '',
      cpwd: '',
      salename: '',
      loginaccount: '',
      role: '',
      updatetime: '',
      state: '',
      openState: false
    }
  },
  getters: {
    accountList: state => state.accountList
  },
  mutations: {
    [ACCOUNT_LIST] (state, data) {
      state.accountList = data
    },
    [ACCOUNT_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [ACCOUNT_DATA_COUNT] (state, data) {
      state.accountDataCount = data
    },
    [ACCOUNT_HISTORY_DATA] (state, data) {
      state.accountHistoryData = data
    },
    [ACCOUNT_PAGE_SIZE] (state, data) {
      state.accountPageSize = data
    }
  },
  actions: {
    async getAccountList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getAccountTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          token: item.token,
          ctime: item.ctime,
          uname: item.uname,
          tel: item.tel.toString(),
          password: item.password,
          // cpwd: item.cpwd,
          salename: item.salename,
          // loginaccount: item.tel,
          role: item.role.toString(),
          pid: item.pid,
          pname: item.pname,
          img: item.img,
          gname: item.gname,
          updatetime: item.updatetime,
          state: item.state,
          openState: item.state === 0
        }]
      })
      commit(ACCOUNT_LIST, newData)
      commit(ACCOUNT_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.account.accountPageSize) {
        commit(ACCOUNT_HISTORY_DATA, newData)
      } else {
        commit(ACCOUNT_HISTORY_DATA, newData.slice(0, rootState.account.accountPageSize))
      }
      return resp
    },
    async editAccountData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editAccountRecord(data)
      return resp
    },
    async editSysAccountData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editSysAccountRecord(data)
      return resp
    },
    async getAccountCurrentRow ({ commit, rootState }, row) {
      commit(ACCOUNT_CURRENT_ROW, row)
    },
    async changeAccountPage ({ commit, rootState }, index) {
      let data = rootState.account.accountList
      let _start = (index - 1) * rootState.account.accountPageSize
      let _end = index * rootState.account.accountPageSize
      commit(ACCOUNT_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeAccountPageSize ({ commit, rootState }, value) {
      commit(ACCOUNT_PAGE_SIZE, value)
    }
  }
}
